package org.demo.treeeditor;

import org.boxlayout.gui.BoxLayoutEngine;
import org.boxlayout.gui.VisualElement;
import org.boxlayout.gui.layout.LayoutFactory;
import org.boxlayout.gui.type.ColorEnum;

public class TextPrompt {

    BoxLayoutEngine ble;
    VisualElement editedElement;
    VisualElement visual;


    VisualElement leftText;
    VisualElement spaceText;
    VisualElement cursor;
    VisualElement rightText;

    ColorEnum currentColor = ColorEnum.GRAY;

   
    public void init(){

	leftText = ble.createElement().setAlpha(0f);
	spaceText = ble.createElement().setAlpha(0f).setPreferedWidth(0f).setPreferedHeight(0.2f);
	cursor= ble.createElement().setLayout(LayoutFactory.getInstance().getVerticalLayout()).setAlpha(0f)
	.add(ble.createElement().setAlpha(0f))//spacer
	.add(ble.createElement().setColor(ColorEnum.BLACK))
	;
	rightText = ble.createElement().setAlpha(0f);

	visual = ble.createElement().setColor(currentColor).setLayout(LayoutFactory.getInstance().getHorizontalLayout());
	//visual.setPreferedHeight(1.0f);
	//visual.setPreferedWidth(0.1f);
	//ble.getRootElement().add(visual);
	visual.add(leftText).add(spaceText).add(cursor)
	//.add(rightText)editedElement
	;
	visual.setEnable(false);

    }

    public VisualElement getVisual() {
	return visual;
    }

    public void type(String string) {
	leftText.setText(leftText.getText()+string);
	if(string.equalsIgnoreCase(" ")){
	    spaceText.setPreferedWidth(spaceText.getPreferedWidth()+0.04f);
	    spaceText.setEnable(true);
	}else{
	    spaceText.setPreferedWidth(0f);
	    spaceText.setEnable(false);
	}
    }

    public void clear() {
	leftText.setText("");
	rightText.setText("");
    }



    public void moveCursorToEnd() {
	leftText.setText(leftText.getText()+rightText.getText());
	rightText.setText("");
    }

    public void setBoxLayoutEngine(BoxLayoutEngine ble) {
	this.ble= ble;
    }

    public void editElement(VisualElement node) {	    	
	clear();
	editedElement = node;
	
	editedElement.setVisible(false);

	cursor.setTextSize(editedElement.getTextSize());
	cursor.getChilds().get(0).setPreferedHeight(editedElement.getHeight()*0.25f);
	cursor.getChilds().get(1).setPreferedHeight(editedElement.getHeight()*0.75f).setPreferedWidth(editedElement.getHeight()*0.05f);
	leftText.setText(editedElement.getText());
	leftText.setPaddingLeft(editedElement.getPaddingLeft());
	leftText.setPaddingTop(editedElement.getPaddingTop());
	leftText.setPaddingBottom(editedElement.getPaddingBottom());
	leftText.setTextSize(editedElement.getTextSize());

	rightText.setPaddingTop(editedElement.getPaddingTop());
	rightText.setPaddingBottom(editedElement.getPaddingBottom());
	rightText.setTextSize(editedElement.getTextSize());

	visual.setX(editedElement.getAbsoluteX());
	visual.setY(editedElement.getAbsoluteY());
	visual.setPreferedHeight(editedElement.getHeight());
	visual.setPreferedWidth(editedElement.getWidth());
	visual.setEnable(true);

    }

    public void save(){
	editedElement.setText(leftText.getText()+rightText.getText());
	editedElement.setVisible(true);
    }

    public void hide(){
	visual.setEnable(false);
	editedElement.setVisible(true);
    }

    public void backSpace() {
	Integer length = leftText.getText().length();
	if(length>0){
	    leftText.setText(leftText.getText().substring(0,length-1));
	}
    }



}
